package com.nuanshui.heatedloan.order.integration.execute;

import com.google.common.collect.Maps;
import com.nuanshui.heatedloan.dubbo.DubboResponse;
import com.nuanshui.heatedloan.order.enums.OrderOptionType;
import com.nuanshui.heatedloan.order.integration.form.OrderExecuteForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;

import java.util.HashMap;
import java.util.Map;

public class OrderExecuteFacadeImpl implements OrderExecuteFacade {
	
	
	
	private OrderExecute orderExecute;

	protected Logger logger = LoggerFactory.getLogger("appointment_order");

	public static final String PLACE_ORDER_EXECUTE_PREFIX = "_PLACE_ORDER_EXECUTE_PREFIX";

	public static final String CALCEL_ORDER_EXECUTE_PREFIX = "_CALCEL_ORDER_EXECUTE_PREFIX";

	public static final String VERIFY_ORDER_EXECUTE_PREFIX = "_VERIFY_ORDER_EXECUTE_PREFIX";

	public static final String PAY_SUCCESS_ORDER = "_PAY_SUCCESS_ORDER";


	private HashMap<String, OrderExecute> orderExecuteCache = Maps.newHashMap();

	private OrderExecuteRegister orderExecuteRegister = new OrderExecuteRegister();

	public OrderExecuteRegister getOrderExecuteRegister() {
		return orderExecuteRegister;
	}

	public HashMap<String, OrderExecute> getOrderExecuteCache() {
		return orderExecuteCache;
	}

	public void setOrderExecuteCache(
			HashMap<String, OrderExecute> orderExecuteCache) {
		this.orderExecuteCache = orderExecuteCache;
	}

	public DubboResponse executePlaceOrder(OrderExecuteForm form){
		//开始执行
		return null;
	}
	

	@Override
	public Map<String, Object> listAppointmentOrder(Integer orderType, Integer orderListType, String userId, Pageable pager) {
		OrderExecute orderExecute = orderExecuteRegister.getOrderExecute(OrderOptionType.getOrderOptionType(orderType).getType() + "");
		OrderExecuteForm form = new OrderExecuteForm();
		form.setPage(pager);
		form.setUserId(userId);
		form.setOrderListType(orderListType);
		return orderExecute.listAppointmentOrderExecute(form);
	}

	@Override
	public Map<String, Object> getAppointmentOrderDetail(String orderNum, String userId) {
		OrderExecute orderExecute = orderExecuteRegister.getOrderExecute(getOrderTypeByOrderNum(orderNum) + "");
		return orderExecute.getAppointmentOrderDetailExecute(orderNum, userId);
	}

	@Override
	public void cancelAppointmentOrder(String orderNum, String cancleReason , String userId) {

		OrderExecuteForm form = new OrderExecuteForm();
		int orderType = getOrderTypeByOrderNum(orderNum);
		form.setOrderNum(orderNum);
		form.setCancelReason(cancleReason);
		form.setUserId(userId);
		form.setOrderType(orderType);
	}





	@Override
	public DubboResponse expireOrder(String orderNum) {
		OrderExecute orderExecute = orderExecuteRegister.getOrderExecute(getOrderTypeByOrderNum(orderNum) + "");
		OrderExecuteForm form = new OrderExecuteForm();
		form.setOrderNum(orderNum);
		return orderExecute.expireOrder(form).getDubboResponse();
	}

	@Override
	public DubboResponse overtimeOrder(String orderNum) {
		OrderExecute orderExecute = orderExecuteRegister.getOrderExecute(getOrderTypeByOrderNum(orderNum) + "");
		OrderExecuteForm form = new OrderExecuteForm();
		form.setOrderNum(orderNum);
		return orderExecute.overtimeOrder(form).getDubboResponse();
	}

	@Override
	public DubboResponse paySuccess(String orderNum) {
		OrderExecuteForm form = new OrderExecuteForm();
		form.setOrderNum(orderNum);
		form.setOrderType(getOrderTypeByOrderNum(orderNum));
		return null;
	}

	/**
	 * 根据订单号获取要操作的类型
	 * @Author: Gonzo
	 * @Date 2017/3/1718:56
	 * @since 1.4.2
	 */
	private Integer getOrderTypeByOrderNum(String orderNum){
		//判断订单倒数两位后缀
		String suffix;
		if (orderNum.endsWith("t")) {
			suffix = orderNum.substring(orderNum.length() - 3, orderNum.length() - 1);
		} else {
			suffix = orderNum.substring(orderNum.length() - 2, orderNum.length());
		}

		int suffixInt = Integer.parseInt(suffix);
		return OrderOptionType.NONE.getType();
	}
}
